package com.meiyou.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.meiyou.mapper.MeiyouSettingMapper;
import com.meiyou.mapper.MeiyouShareMapper;
import com.meiyou.mapper.MeiyouUserMapper;
import com.meiyou.pojo.MeiyouSetting;
import com.meiyou.pojo.MeiyouSettingExample;
import com.meiyou.pojo.MeiyouShare;
import com.meiyou.pojo.MeiyouShareExample;
import com.meiyou.service.ShareService;
import com.meiyou.utils.Msg;
@Service
public class ShareServiceImpl implements ShareService{
	
	
	@Autowired
	MeiyouShareMapper shareMapper;
	@Autowired
	MeiyouUserMapper userMapper;
	@Autowired
	MeiyouSettingMapper settingMapper;
	
    @Resource
    private MeiyouShareMapper meiyouShareMapper;
	public int insShare(MeiyouShare meiyouShare) {
		return meiyouShareMapper.insert(meiyouShare);
	}
	
	
	public Msg selShareCode(String code) {
		Msg msg;
		MeiyouShareExample example = new MeiyouShareExample();
		MeiyouShareExample.Criteria criteria = example.createCriteria();
		criteria.andSCodeEqualTo(code);
		// TODO Auto-generated method stub
		List<MeiyouShare> shares = shareMapper.selectByExample(example);
		if(shares.size()==0||shares==null) {
			msg = Msg.fail();
			msg.setMsg("无此分享码");
		}else {
			MeiyouShare share = new MeiyouShare();
			share.setsState("1");
			if(shareMapper.updateByExample(share, example)>=1) {
				MeiyouSettingExample settingExample = new MeiyouSettingExample();
				MeiyouSettingExample.Criteria settingCriteria = settingExample.createCriteria();
				settingCriteria.andJNameEqualTo("fenxiangjin");
				List<MeiyouSetting> settings = settingMapper.selectByExample(settingExample);
				String shareMeijin = "0";
				if(settings.size()>0) {
					shareMeijin = settings.get(0).getjValue();
				}
				 
				if(userMapper.updAddMeijin(Integer.parseInt(shareMeijin), shares.get(0).getsUCount())>=1) {
					msg = Msg.success();
				}else {
					msg = Msg.fail();
				}
				
			}else {
				msg = Msg.fail();
			}
		}
		return msg;
	}

}
